package com.region.http.client.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Request parameter set
 *
 * @author liujieyu
 * @date 2023/7/8 17:21
 * @desciption
 */
public class RequestParameter {

    /**
     * Collection of request header parameters, Source parameter @ Header annotations and RegionContextHolder. GetHeader ()
     */
    private Map<String, Object> headers;

    /**
     * A collection of url path parameters and a collection of form parameters (Content-Type: multipart/form-data only)
     */
    private Map<String, Object> form;

    /**
     * Store data marked as body
     */
    private Object body;

    /**
     * Add extension parameter
     */
    private List<Object> extendedParameter;

    /**
     * The primitive args
     */
    private Object[] PrimitiveArgs;

    public Map<String, Object> getHeaders() {
        return headers;
    }

    private void setHeaders(Map<String, Object> headers) {
        this.headers = headers;
    }

    public Map<String, Object> getForm() {
        return form;
    }

    private void setForm(Map<String, Object> form) {
        this.form = form;
    }

    public Object getBody() {
        return body;
    }

    private void setBody(Object body) {
        this.body = body;
    }

    public List<Object> getExtendedParameter() {
        return extendedParameter;
    }

    public void setExtendedParameter(List<Object> extendedParameter) {
        this.extendedParameter = extendedParameter;
    }

    public Object[] getPrimitiveArgs() {
        return PrimitiveArgs;
    }

    private void setPrimitiveArgs(Object[] primitiveArgs) {
        PrimitiveArgs = primitiveArgs;
    }

    /**
     * Create Builder
     */
    public static RequestParameterBuilder builder() {
        return new RequestParameterBuilder();
    }

    /**
     * Request Parameter Builder
     */
    public static class RequestParameterBuilder {

        private RequestParameter requestParameter = new RequestParameter();

        public RequestParameterBuilder() {
            super();
        }

        public RequestParameterBuilder headers(Map<String, Integer> header, Object[] args) {
            setPrimitiveArgs(args);
            Map<String, Object> data = new HashMap<>(8);
            if (header != null && !header.isEmpty()) {
                for (Map.Entry<String, Integer> entry : header.entrySet()) {
                    String name = entry.getKey();
                    Object value = args[entry.getValue().intValue()];
                    data.put(name, value);
                }
                Map<String, Object> headers = requestParameter.getHeaders();
                if (headers == null) {
                    this.requestParameter.setHeaders(data);
                } else {
                    // overlay parameter
                    headers.putAll(data);
                }
            }
            return this;
        }

        public RequestParameterBuilder headers(Map<String, String> header) {
            if (header == null) return this;
            Map<String, Object> headers = requestParameter.getHeaders();
            if (headers == null) {
                headers = new HashMap<>(8);
            }
            for (Map.Entry<String, String> entry : header.entrySet()) {
                headers.put(entry.getKey(), entry.getValue());
            }
            requestParameter.setHeaders(headers);
            return this;
        }

        public RequestParameterBuilder form(Map<String, Integer> form, Object[] args) {
            setPrimitiveArgs(args);
            if (form != null && !form.isEmpty()) {
                Map<String, Object> data = new HashMap<>(8);
                for (Map.Entry<String, Integer> entry : form.entrySet()) {
                    String name = entry.getKey();
                    Object value = args[entry.getValue().intValue()];
                    data.put(name, value);
                }
                Map<String, Object> formData = this.requestParameter.getForm();
                if (formData == null) {
                    this.requestParameter.setForm(data);
                } else {
                    formData.putAll(data);
                }
            }
            return this;
        }

        public RequestParameterBuilder form(Map<String, String> form) {
            if (form == null) return this;
            Map<String, Object> formData = requestParameter.getForm();
            if (formData == null) {
                formData = new HashMap<>(8);
            }
            for (Map.Entry<String, String> entry : form.entrySet()) {
                formData.put(entry.getKey(), entry.getValue());
            }
            requestParameter.setForm(formData);
            return this;
        }

        public RequestParameterBuilder body(Integer body, Object[] args) {
            setPrimitiveArgs(args);
            if (body != null) {
                this.requestParameter.setBody(args[body.intValue()]);
            }
            return this;
        }

        public RequestParameterBuilder body(String body) {
            if (body == null) return this;
            this.requestParameter.setBody(body);
            return this;
        }

        private void setPrimitiveArgs(Object[] args) {
            if (this.requestParameter.getPrimitiveArgs() == null && args != null) {
                this.requestParameter.setPrimitiveArgs(args);
            }
        }

        public RequestParameter build() {
            return requestParameter;
        }
    }
}
